﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Module.TCenter
{
    public partial class TCenterHire : System.Web.UI.Page
    {
        private int userId;
        private int cUserId;
        private int venuesId;
        protected string ModuleId;
        private Jy123.Model.TCenter_Main TCenter;
        private Jy123.Model.Module_Main module;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;
        private string[] strContent = { "我的中心", "中心升级", "训练设备", "组件排行", "训练信息", "组件维护", "商业活动" };
        private string[] strCss = { "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu3" };
        private string[] strHeaderCss = { "", "", "NavIcon", "" };


        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            if (!Page.IsPostBack)
            {
                cUserId = int.Parse(strUserIdFromCookie);
                LoadInitialValue();
            }
        }

        private void LoadInitialValue()
        {
            string key = "TCenterId";
            string venuesIdString = Request.QueryString[key];
            if (!string.IsNullOrEmpty(venuesIdString))
            {
                venuesId = int.Parse(venuesIdString);
                ModuleId = venuesIdString;
                Jy123.BLL.TCenter_Main BLLVenues = new Jy123.BLL.TCenter_Main();

                Jy123.BLL.Module_Main BLLModuleMain = new Jy123.BLL.Module_Main();
                Jy123.BLL.Club.CModule BLLCModule = new Jy123.BLL.Club.CModule();
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

                TCenter = BLLVenues.GetModel(venuesId);
                userId = TCenter.FUser.Value;
                user = BLLUser.GetModel(userId);
                level = BLLLevel.GetModel(TCenter.FLevel.Value);
                module = BLLModuleMain.GetModel(venuesId);
                BindHTML();
            }
            else
            {
                //找不到UserId，返回首页,目前暂时返回空值
            }
        }

        private void BindHTML()
        {
            string strHTML = Cache["venueshire"].ToString();
            Jy123.Web.AppCode.Module appModule = new Jy123.Web.AppCode.Module();
            string strHeader = Cache["header"].ToString();
            string headerString = appModule.BindHeader(strHeader, strHeaderCss);
            string strLeft = Cache["leftmenu"].ToString();
            string strLeftLi = Cache["leftmenuli"].ToString();
            string strTail = "?TCenterId=" + TCenter.ID.ToString();
            string[] strHref = { "MyTCenter.aspx" + strTail, "UpdateTCenter.aspx" + strTail, "TCenterAnnexe.aspx" + strTail, "TCenterRank.aspx" + strTail, "TrainInfo.aspx" + strTail, "TCenterRepair.aspx" + strTail, "#" };
            string[] strNoRaceHref = { "MyTCenter.aspx" + strTail, "UpdateTCenter.aspx" + strTail, "TCenterAnnexe.aspx" + strTail, "TCenterRank.aspx" + strTail, "javascript:$.moonbox.warning('您的训练中心没有进行俱乐部进行训练！')", "TCenterRepair.aspx" + strTail, "#" };
            string leftMenuString;
            if (TCenter.CurrentClubs.Value > 0)
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strHref, strCss);
            }
            else
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strNoRaceHref, strCss);
            }
            string strClub = Cache["contentclub"].ToString();
            string contentString = appModule.BindContentClub(strClub, module, TCenter, user, level);
            string strMain = Cache["mainmenu"].ToString();
            string strDiv = Cache["mainmenusubdiv"].ToString();
            string strLi = Cache["mainmenusubdivli"].ToString();
            string mainMenuString = appModule.BindMainMenu(strMain, strDiv, strLi, cUserId);

            strHTML = strHTML.Replace("$Header$", headerString);
            strHTML = strHTML.Replace("$MainMenu$", mainMenuString);
            if (cUserId == userId)
            {
                strHTML = strHTML.Replace("$LeftMenu$", leftMenuString);
            }
            else
            {
                strHTML = strHTML.Replace("$LeftMenu$", string.Empty);
            }
            strHTML = strHTML.Replace("$ContentClub$", contentString);
            strHTML = strHTML.Replace("$Footer$", Cache["footer"].ToString());
            strHTML = strHTML.Replace("$VenuesName$", TCenter.TName);
            strHTML = strHTML.Replace("$ModuleId$", TCenter.ID.ToString());
            Jy123.BLL.Public_Hire BLLHire = new Jy123.BLL.Public_Hire();
            List<Jy123.Model.Public_Hire> listHire = BLLHire.GetModelList("FJType = " + module.FJType.Value.ToString());
            StringBuilder sbHire = new StringBuilder();
            foreach (Jy123.Model.Public_Hire hire in listHire)
            {
                string strHire = Cache["venueshireli"].ToString();
                strHire = strHire.Replace("$ImageSrc$", hire.Logo);
                strHire = strHire.Replace("$CheckBoxId$", "checkBox" + hire.ID.ToString());
                strHire = strHire.Replace("$Value$", hire.ID.ToString());
                strHire = strHire.Replace("$HireIdHidden$", "hidden" + hire.ID.ToString());
                strHire = strHire.Replace("$HireName$", hire.HName);
                strHire = strHire.Replace("$HirePrice$", (Convert.ToInt32(hire.Price.Value)).ToString() + "/小时");
                sbHire.Append(strHire);
            }
            if (string.IsNullOrEmpty(sbHire.ToString()))
            {
                strHTML = strHTML.Replace("$Li$", "没有租用项目");
            }
            else
            {
                strHTML = strHTML.Replace("$Li$", sbHire.ToString());
            }

            Response.Write(strHTML);

        }
    }
}
